#ifndef SP_DRIVER_ATA_PIO_H
#define SP_DRIVER_ATA_PIO_H

#include <system/mod.h>
#include <klibc/ctype.h>

/*
 * ATA ports
 */
/*;       1f0       r/w       data register, the bytes are written/read here
;       1f1       r         error register  (look these values up yourself)
;       1f2       r/w       sector count, how many sectors to read/write
;       1f3       r/w       sector number, the actual sector wanted
;       1f4       r/w       cylinder low, cylinders is 0-1024
;       1f5       r/w       cylinder high, this makes up the rest of the 1024
;       1f6       r/w       drive/head
;                              bit 7 = 1
;                              bit 6 = 0
;                              bit 5 = 1
;                              bit 4 = 0  drive 0 select
;                                    = 1  drive 1 select
;                              bit 3-0    head select bits
;       1f7       r         status register
;                              bit 7 = 1  controller is executing a command
;                              bit 6 = 1  drive is ready
;                              bit 5 = 1  write fault
;                              bit 4 = 1  seek complete
;                              bit 3 = 1  sector buffer requires servicing
;                              bit 2 = 1  disk data read corrected
;                              bit 1 = 1  index - set to 1 each revolution
;                              bit 0 = 1  previous command ended in an error
;       1f7       w         command register
;                            commands:
;                              50h format track
;                              20h read sectors with retry
;                              21h read sectors without retry
;                              22h read long with retry
;                              23h read long without retry
;                              30h write sectors with retry
;                              31h write sectors without retry
;                              32h write long with retry
;                              33h write long without retry*/


typedef struct
{
		uint8_t ERR:1;		// Error flag (when set). Send a new command to clear it (or nuke it with a Software Reset).
		uint8_t NUL:2;
		uint8_t DRQ:1;		// Set when the drive has PIO data to transfer, or is ready to accept PIO data.
		uint8_t SRV:1;		// Overlapped Mode Service Request
		uint8_t DF:1;		// Drive Fault Error (does not set ERR!)
		uint8_t RDY:1;		// Bit is clear when drive is spun down, or after an error. Set otherwise.
		uint8_t BSY:1;		// Drive is preparing to accept/send data -- wait until this bit clears. If it never
							// clears, do a Software Reset. Technically, when BSY is set, the other bits in the
							// Status byte are meaningless.
} ata_pio_status_t;



extern module_t ata_pio_module;

void ata_pio_reset();
void ata_pio_test();
uint32_t ata_pio_init();
uint32_t ata_pio_shut();
uint32_t ata_pio_where(uint8_t, uint32_t, uint64_t);
uint32_t ata_pio_read(uint8_t, uint64_t, char *, uint32_t);
uint32_t ata_pio_write(uint8_t, uint64_t, char *, uint32_t);
uint32_t ata_pio_awrite(struct vfs_node_s *, char *, uint32_t, uint64_t);
uint32_t ata_pio_aread(struct vfs_node_s *, char *, uint32_t, uint64_t);
uint32_t ata_pio_open();
uint32_t ata_pio_close();

#endif
